package jifenbao.com.jifenbao;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by longteng on 2016-6-3.
 */
public class ViewPagerTest extends Activity {
    public ViewPager viewpager;
    //private View view1,view2;
    private List<View> viewList;
    private List<String> titleList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager_main);

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        LayoutInflater inflater = getLayoutInflater();
//        view1 = inflater.inflate(R.layout.activity_point_record,null);
//        view2 = inflater.inflate(R.layout.activity_point_record,null);
        ListView listView1 = (ListView) (inflater.inflate(R.layout.activity_point_record, null)).findViewById(R.id.point_record);
        ListView listView2 = (ListView) (inflater.inflate(R.layout.activity_point_record, null)).findViewById(R.id.point_record);

        viewList = new ArrayList<View>();
        viewList.add(listView1);
        viewList.add(listView2);

        titleList = new ArrayList<String>();
        titleList.add("可用");
        titleList.add("不可用");

        List<Map<String,String>> recordlist = new ArrayList<>();
        for (int i=0;i<30;i++) {
            Map<String, String> map1 = new HashMap<>();
            map1.put("record_name", "扫码积分");
            map1.put("record_count", "+" + 10);
            map1.put("record_time", "2016/3/25 19:00");
            recordlist.add(map1);
        }

        SimpleAdapter myAdapter = new SimpleAdapter(this,recordlist,R.layout.activity_point_record_item,
                new String[]{"record_name","record_count","record_time","record_count_2"},
                new int[]{R.id.record_name,R.id.record_count,R.id.record_time,R.id.record_count_2});
        listView1.setAdapter(myAdapter);

        SimpleAdapter myAdapter2 = new SimpleAdapter(this,recordlist,R.layout.activity_point_record_item,
                new String[]{"record_name","record_count","record_time","record_count_2"},
                new int[]{R.id.record_name,R.id.record_count,R.id.record_time,R.id.record_count_2});
        listView2.setAdapter(myAdapter2);

        viewpager.setAdapter(new MyPagerAdapter(viewList));
    }

    public class MyPagerAdapter extends PagerAdapter{

        private List<View> viewList;

        public MyPagerAdapter(List<View> viewList) {

            this.viewList = viewList;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {
           return titleList.get(position);
        }
    }
}
